<template>
  <a-card :bordered="false">
      <!-- 查询区域 -->
    <div class="table-page-search-wrappe">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="申请时间" :labelCol="labelCol" :wrapperCol="wrapperCol" class="form-item">
                <a-range-picker
                  v-model="createTimeRange"
                  :allowClear="false"
                  format="YYYY-MM-DD"
                  :placeholder="['开始时间', '结束时间']"
                  @change="onCreateTimeChange" />
                </a-form-item>
            </a-col>
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="单据编号" :labelCol="labelCol" :wrapperCol="wrapperCol" class="form-item">
              <j-input placeholder="请输入单据编号" v-model="queryParam.billNo"></j-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-model-item label="申请人">
              <select-user-by-dep
                placeholder="请选择申请人"
                v-model="queryParam.proposer" />
            </a-form-model-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="申请公司">
              <select-Company-input  v-model="queryParam.applicant" multi/>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="费用所属团队">
              <select-team-input v-model="queryParam.teamId" :teamType="0" :tdlx="3" multi/>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="预付款金额" style="height: 32px;">
              <a-input-number placeholder="请输入最小值" class="query-group-cust" :precision="0" v-model="queryParam.payAmount_begin" style="width:calc(50% - 15px);"></a-input-number>
              <span class="query-group-split-cust"></span>
              <a-input-number placeholder="请输入最大值" class="query-group-cust" :precision="0" :min="queryParam.payAmount_begin" v-model="queryParam.payAmount_end" style="width:calc(50% - 15px);"></a-input-number>
            </a-form-item>
          </a-col>
          <!-- <template v-if="toggleSearchStatus"></template> -->
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="createTimeRange=[],searchReset()" icon="reload" style="margin-left: 8px">重置</a-button>
              <!-- <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a> -->
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->
    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button type="primary" icon="download" @click="handleExportXls('预付款台账')" :disabled="isDisabledAuth('ErpPaymentSlipReportList:export')">导出</a-button> 
      <a-popover title="自定义列" trigger="click" placement="leftBottom">
        <template slot="content">
          <a-checkbox-group @change="onColSettingsChange" v-model="settingColumns" :defaultValue="settingColumns">
            <a-row>
              <template v-for="(item,index) in defColumns">
                <template v-if="item.key!='rowIndex'&& item.dataIndex!='action'">
                  <a-col :span="12" :key="index"><a-checkbox :value="item.dataIndex">{{ item.title }}</a-checkbox></a-col>
                </template>
              </template>
            </a-row>
          </a-checkbox-group>
        </template>
        <a style="float: right;"><a-icon type="setting" :style='iconStyle'/></a>
      </a-popover>
    </div>
    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>
      <a-table
        ref="table"
        size="middle"
        :scroll="{x: scrollX}"
        :components="draggableComponent"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange">
        <a slot="billNo" slot-scope="text, record" @click="getDetail(record)">{{ text }}</a>
        <a slot="payAmount" slot-scope="text, record" @click="getDetail(record)">{{ text }}</a>
        <a slot="onChargedAmount" slot-scope="text, record" @click="getDetailList(record)">{{ text }}</a>
        <template slot="status" slot-scope="status">
          <a-badge :status="status | statusTypeFilter" :text="status | statusFilter"/>
        </template>
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>
      </a-table>
    </div>
    <ErpFinanceReimbursementRelBillListPenetrate ref="reimbursementPaymentPenetrate"></ErpFinanceReimbursementRelBillListPenetrate>
    <ErpFinancePaymentBillPenetrate ref="paymentBillPenetrate"></ErpFinancePaymentBillPenetrate>
  </a-card>
</template>
<script>
  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import {JeecgListMixin} from '@/mixins/JeecgListMixin'
  import SelectCustomerInput from '@/views/scy/components/SelectCustomerInput'
  import {DisabledAuthFilterMixin} from '@/mixins/DisabledAuthFilterMixin'
  import moment from 'moment'
  import SelectCompanyInput from '@/views/scy/components/SelectCompanyInput'
  import SelectTeamInput from '@/views/scy/components/SelectTeamInput'
  import { downFile } from '@/api/manage'
  import ErpFinancePaymentBillPenetrate from '@/views/scy/penetrate/ErpFinancePaymentBillPenetrate'
  import ErpFinanceReimbursementRelBillListPenetrate from '@/views/scy/penetrate/ErpFinanceReimbursementRelBillListPenetrate'
  import SelectUserByDep from '@/views/scy/components/SelectUserByDep'
  export default {
    name: 'ErpFinancePaymentSlipList',
    mixins:[JeecgListMixin, mixinDevice, DisabledAuthFilterMixin],
    components: {
      SelectCustomerInput,
      SelectCompanyInput,
      SelectUserByDep,
      SelectTeamInput,
      ErpFinancePaymentBillPenetrate,
      ErpFinanceReimbursementRelBillListPenetrate
    },
    data () {
      return {
        queryParam:{
          createTime_begin:moment().startOf('year').format('YYYY-MM-DD HH:mm:ss'),
          createTime_end:moment(new Date()).format('YYYY-MM-DD HH:mm:ss')
        },
        createTimeRange:[moment().startOf('year').format('YYYY-MM-DD HH:mm:ss'),moment(new Date()).format('YYYY-MM-DD HH:mm:ss')],
        description: '预付款台账',
        //表头
        columns:[],
        //列设置
        settingColumns:[],
        // 列定义
        defColumns: [
          {
            title: '序号',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'单据编号',
            align:"center",
            dataIndex: 'billNo',
            width:250,
            scopedSlots: { customRender: 'billNo' },
            ellipsis: true,
          },
          {
            title:'申请人',
            align:"center",
            width:250,
            dataIndex: 'proposer_dictText',
            ellipsis: true,
          },
          {
            title:'申请公司',
            align:"center",
            width:250,
            dataIndex: 'applicant_dictText',
            ellipsis: true,
          },
          {
            title:'费用所属团队',
            align:"center",
            dataIndex: 'teamId_dictText',
            ellipsis: true,
            width:250,
          },
          {
            title:'预付金额（元）',
            align:"center",
            dataIndex: 'payAmount',
            scopedSlots: { customRender: 'payAmount' },
            ellipsis: true,
            width:250,
          },
          {
            title:'已冲账金额（元）',
            align:"center",
            dataIndex: 'onChargedAmount',
            scopedSlots: { customRender: 'onChargedAmount' },
            ellipsis: true,
            width:250,
          },
          {
            title:'剩余未冲账金额（元）',
            align:"center",
            dataIndex: 'unChargedAmount',
            ellipsis: true,
            width:250,
          },
        ],
        url: {
          list: "/api/erp/finance/report/erpPaymentSlipReport/list", 
          exportXlsUrl: "/api/erp/finance/report/erpPaymentSlipReport/exportXls",
        },
        //代码生成后需手动设置流程编码
        dictOptions:{},
        superFieldList:[],
      }
    },
    created() {
      this.getSuperFieldList();
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      searchQuery() {
        this.onClearSelected();
        this.loadData(1)
      },
      handleExportXls(fileName) {
        if (!fileName || typeof fileName !== 'string') {
          fileName = '导出文件'
        }
        let param = this.getQueryParams()
        if (this.selectedRowKeys && this.selectedRowKeys.length > 0) {
          param['selections'] = this.selectedRowKeys.join(',')
        }
        downFile(this.url.exportXlsUrl, param).then(data => {
          if (!data) {
            this.$message.warning('文件下载失败')
            return
          }
          if (typeof window.navigator.msSaveBlob !== 'undefined') {
            window.navigator.msSaveBlob(
              new Blob([data], {
                type: 'application/vnd.ms-excel'
              }),
              fileName + '.xls'
            )
          } else {
            let url = window.URL.createObjectURL(
              new Blob([data], {
                type: 'application/vnd.ms-excel'
              })
            )
            let link = document.createElement('a')
            link.style.display = 'none'
            link.href = url
            link.setAttribute('download', fileName + '.xls')
            document.body.appendChild(link)
            link.click()
            document.body.removeChild(link) // 下载完成移除元素
            window.URL.revokeObjectURL(url) // 释放掉blob对象
          }
        })
      },
      getDetailList(record){
        //先清空数据
        this.$refs.reimbursementPaymentPenetrate.dataSource = []
        this.$refs.reimbursementPaymentPenetrate.ipagination.total = 0
        //查询数据
        this.$refs.reimbursementPaymentPenetrate.loading = true
        this.$refs.reimbursementPaymentPenetrate.relBillId = record.id
        this.$refs.reimbursementPaymentPenetrate.searchResets()
        //打开弹窗
        this.$refs.reimbursementPaymentPenetrate.visible = true
      },
      getDetail(record){
        this.$refs.paymentBillPenetrate.initData(record.id);
        this.$refs.paymentBillPenetrate.visible = true;
      },
      searchReset() {
        this.queryParam = {
          createTime_begin:moment().startOf('year').format('YYYY-MM-DD HH:mm:ss'),
          createTime_end:moment(new Date()).format('YYYY-MM-DD HH:mm:ss')
        }
        this.createTimeRange=[moment().startOf('year').format('YYYY-MM-DD HH:mm:ss'),moment(new Date()).format('YYYY-MM-DD HH:mm:ss')]
        this.onClearSelected();
        this.loadData(1)
      },
      onCreateTimeChange(dateString) {
        this.queryParam.createTime_begin=moment(dateString[0]).format('YYYY-MM-DD')+" 00:00:00";
        this.queryParam.createTime_end=moment(dateString[1]).format('YYYY-MM-DD')+" 23:59:59";
      },
      initDictConfig(){
      },
      getSuperFieldList(){
        let fieldList=[];
        fieldList.push({type:'date',value:'createTime',text:'创建日期'})
        fieldList.push({type:'string',value:'handlerId',text:'经办人编号',dictCode:''})
        fieldList.push({type:'string',value:'handlerDeptId',text:'经办人部门ID',dictCode:''})
        fieldList.push({type:'string',value:'billNo',text:'单据编号',dictCode:''})
        fieldList.push({type:'string',value:'remark',text:'备注',dictCode:''})
        fieldList.push({type:'string',value:'applicant',text:'申请公司',dictCode:''})
        fieldList.push({type:'string',value:'isRelContract',text:'是否关联合同',dictCode:'yes_no'})
        fieldList.push({type:'string',value:'payMode',text:'支付方式(字典)',dictCode:'payment_type'})
        fieldList.push({type:'string',value:'payeeName',text:'收款方名称',dictCode:''})
        fieldList.push({type:'string',value:'payeeBankName',text:'收款方开户行',dictCode:''})
        fieldList.push({type:'string',value:'payeeAccountNo',text:'收款方账户',dictCode:''})
        fieldList.push({type:'string',value:'payeeContacts',text:'收款联系人',dictCode:''})
        fieldList.push({type:'string',value:'payeeContactInfo',text:'收款联系方式',dictCode:''})
        fieldList.push({type:'string',value:'payRemark',text:'打款备注',dictCode:''})
        fieldList.push({type:'Text',value:'attachments',text:'附件',dictCode:''})
        this.superFieldList = fieldList
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>
